package team.southcat.wsyygh.order.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.southcat.wsyygh.common.result.Result;
import team.southcat.wsyygh.common.util.UserInfoUtil;
import team.southcat.wsyygh.model.enums.OrderStatusEnum;
import team.southcat.wsyygh.model.pojo.order.Order;
import team.southcat.wsyygh.model.pojo.order.OrderVo;
import team.southcat.wsyygh.order.service.OrderService;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/order")
public class OrderApiController {
    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "创建订单")
    @PostMapping("/auth/submitOrder/{scheduleId}/{patientId}")
    public Result submitOrder(
            @PathVariable String scheduleId,
            @PathVariable Long patientId) {
        Long id = orderService.saveOrder(scheduleId, patientId);
        return Result.ok(String.valueOf(id));
    }

    @GetMapping("/auth/getOrderById/{id}")
    public Result getOrderById(@PathVariable long id){
        Order order = orderService.selectById(id);
        return Result.ok(order);
    }

    //订单列表（条件查询带分页）
    @GetMapping("auth/{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit,
                       OrderVo orderVo, HttpServletRequest request) {
        //设置当前用户id
        orderVo.setUserId(Long.valueOf(UserInfoUtil.getUserId(request)));
        Page<OrderVo> pageModel = orderService.selectPage(page, limit,orderVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "获取订单状态")
    @GetMapping("auth/getStatusList")
    public Result getStatusList() {
        return Result.ok(OrderStatusEnum.getStatusList());
    }

    @ApiOperation(value = "取消预约")
    @GetMapping("auth/cancelOrder/{orderId}")
    public Result getStatusList(@PathVariable long orderId) {
        boolean flag = orderService.cancelOrder(orderId);
        return flag ? Result.ok() : Result.fail().message("取消失败");
    }

    @ApiOperation(value = "支付")
    @GetMapping("auth/pay/{orderId}")
    public Result pay(@PathVariable long orderId) {
        boolean flag = orderService.pay(orderId);
        return flag ? Result.ok() : Result.fail().message("取消失败");
    }
}
